package com.zhangxu.microservice.system.web.controller.ae;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.common.result.CommonResult;
import com.zhangxu.microservice.system.biz.dto.ApplicationDTO;
import com.zhangxu.microservice.system.biz.dto.UpdateApplicationDTO;
import com.zhangxu.microservice.system.biz.service.ApplicationService;
import com.zhangxu.microservice.system.dao.condition.ApplicationCondition;
import com.zhangxu.microservice.system.dao.enums.ApplicationStatusEnum;
import com.zhangxu.microservice.system.web.convert.ApplicationWebConverter;
import com.zhangxu.microservice.system.web.request.CreateApplicationReq;
import com.zhangxu.microservice.system.web.request.PageReq;
import com.zhangxu.microservice.system.web.request.UpdateApplicationReq;
import com.zhangxu.microservice.system.web.response.ApplicationResp;
import com.zhangxu.microservice.system.web.response.PageResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统管理控制器
 */
@Tag(name = "系统管理", description = "系统管理相关接口")
@RestController
@RequestMapping("/ae/applications")
@RequiredArgsConstructor
public class ApplicationController {

    private final ApplicationService applicationService;
    private final ApplicationWebConverter applicationWebConverter;

    @Operation(summary = "创建系统", description = "创建新系统")
    @PostMapping
    public CommonResult<Long> create(@RequestBody @Validated CreateApplicationReq createApplicationReq) {
        Long applicationId = applicationService.createApplication(applicationWebConverter.toCreateApplicationDTO(createApplicationReq));
        return CommonResult.success(applicationId);
    }

    @Operation(summary = "更新系统", description = "更新现有系统信息")
    @PutMapping("/{id}")
    public CommonResult<Void> update(@Parameter(description = "系统ID", required = true, example = "1") @PathVariable("id") Long id,
                                     @RequestBody @Validated UpdateApplicationReq updateApplicationReq) {
        UpdateApplicationDTO updateApplicationDTO = applicationWebConverter.toUpdateApplicationDTO(updateApplicationReq);
        updateApplicationDTO.setId(id);
        applicationService.updateApplication(updateApplicationDTO);
        return CommonResult.success();
    }

    @Operation(summary = "删除系统", description = "根据ID删除系统")
    @DeleteMapping("/{id}")
    public CommonResult<Void> deleteSystem(
            @Parameter(description = "系统ID", required = true, example = "1")
            @PathVariable("id") Long id) {
        applicationService.deleteApplication(id);
        return CommonResult.success();
    }

    @Operation(summary = "启用系统", description = "启用指定系统")
    @PostMapping("/{id}/enable")
    public CommonResult<Void> enable(@Parameter(description = "系统ID", required = true, example = "1")
                                     @PathVariable("id") Long id) {
        applicationService.enableApplication(id);
        return CommonResult.success();
    }

    @Operation(summary = "禁用系统", description = "禁用指定系统")
    @PostMapping("/{id}/disable")
    public CommonResult<Void> disable(@Parameter(description = "系统ID", required = true, example = "1")
                                      @PathVariable("id") Long id) {
        applicationService.disableApplication(id);
        return CommonResult.success();
    }

    @Operation(summary = "获取系统详情", description = "根据ID获取系统详情")
    @GetMapping("/{id}")
    public CommonResult<ApplicationResp> get(@Parameter(description = "系统ID", required = true, example = "1")
                                             @PathVariable("id") Long id) {
        ApplicationDTO applicationDTO = applicationService.getApplicationById(id);
        return CommonResult.success(applicationWebConverter.toApplicationResp(applicationDTO));
    }

    @Operation(summary = "系统列表", description = "分页查询系统列表")
    @GetMapping
    public CommonResult<PageResponse<ApplicationResp>> page(@Valid PageReq pageReq,
                                                            @Parameter(description = "系统编码") @RequestParam(value = "code", required = false) String code,
                                                            @Parameter(description = "系统名称") @RequestParam(value = "name", required = false) String name,
                                                            @Parameter(description = "系统状态") @RequestParam(value = "status", required = false) ApplicationStatusEnum status,
                                                            @Parameter(description = "系统负责人用户ID") @RequestParam(value = "ownerId", required = false) Long ownerId) {
        ApplicationCondition condition = new ApplicationCondition();
        condition.setCode(code);
        condition.setName(name);
        condition.setStatus(status);
        condition.setOwnerId(ownerId);
        condition.setPageNum(pageReq.getPageNum());
        condition.setPageSize(pageReq.getPageSize());
        IPage<ApplicationDTO> page = applicationService.page(condition);
        return CommonResult.success(PageResponse.convert(page, applicationWebConverter::toApplicationResp));
    }
}